const path = require('path');
const fs = require('fs');
const md5 = require('md5');
// const createDir = () => {
//
// }

const generaItem = (dir, data, filename = 'index-create.html', tmpFile = 'index-template.html') => {
  if (!fs.existsSync(path.normalize(dir))) {
    return 'Error:路径获取错误！！';
  }
  let tmpStr = fs.readFileSync(path.join(__dirname, `/template/${tmpFile}`));
  tmpStr = tmpStr.toString();
  const keys = Object.keys(data);
  keys.forEach((item) => {
    tmpStr = tmpStr.replace(`@{${item.toUpperCase()}}`, data[item]);
  });
  fs.writeFileSync(path.join(dir, filename), tmpStr);
}

const generaIndex = (params) => {
  const root = params.rootDir;
  fs.writeFileSync(path.normalize(`${root}/generaIndex.text`), `${root}\n陆荣泽逻辑可获利空间`);
};

const uploadImg = (params) => {
  const file = params.path || '';
  const data = fs.readFileSync(file);
  fs.writeFileSync(path.normalize(`F:/logs/cc/img/${md5(file)}.jpg`), data);
}

module.exports = {
  uploadImg,
  generaItem,
  generaIndex,
};
